use Test::More tests => 68;
#use Test::More qw(no_plan);

use Firepear::Carrot::Lexer;

BEGIN {
use_ok( 'Firepear::Carrot::Handler::VerbatimText' );
}

my $mu = ['[',']',';',"'",'-'];

my $h = Firepear::Carrot::Handler::VerbatimText->new( mu => $mu );

isa_ok ($h, 'Firepear::Carrot::Handler::VerbatimText');

is ($h->{mu}{r}, '\;\;\;', 'rem');
is ($h->{mu}{s}, '\;', 'separator');
is ($h->{mu}{q}, "\\'", 'quote');
is ($h->{mu}{z}{o}, '\[', 'rawopen');
is ($h->{mu}{z}{c}, '\]', 'rawclose');

######################################################## 000 - Normal operation
is ($h->status, 0, 'normal ops - 0');

##################################################################### 'handles'
is ($h->handles('[[document'), 1, 'Yes, we handle everything');
is ($h->handles('[[[data'), 1, 'Yes, we handle everything');
is ($h->handles('[section'), 1, 'Yes, we handle everything');
is ($h->handles('text'), 1, 'Yes, we handle everything');
is ($h->handles('bleh]]'), 1, 'Yes, we handle everything');
is ($h->handles(']]]'), 1, 'Yes, we handle everything');
is ($h->handles('bleh]'), 1, 'Yes, we handle everything');

################################################### 001 - Complete node parsing
#
# all these tests are straight from the Text suite, and all the ones it is
# expected to halt on due to markup, we plow through because none of that
# markup is Verbatim closing markup -- the only thing VerbatimText halts
# for. A test for this follows.
#
my $t = "this is a test";
my $l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
is ($h->status, 0, 'normal ops - 0');
my $node = $h->lex($l);
is (defined $node, 1);
is ($h->status, 1, 'node processed completely - 1');
is (ref $node, 'HASH', 'should be a hashref');
is ($node->{content}, 'this is a test');
is ($node->{element}, undef);
is ($node->{refid}, undef);
is (defined @{$node->{attrs}}, '', 'no attrs');
is ($node->{type}, 'VERBATIMTEXT');
is ($node->{finfo}[0], 1, 'file number');
is ($node->{finfo}[1], 1, 'line in file');
is ($l->current, "test", 'Lexer in expected state');

$t = "this is a test\nand so is this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch;
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'node processed completely - multiline');
is ($node->{content}, "this is a test\nand so is this");

$t = "this is a test ;;; with comments! \nand so is this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch;
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'node processed completely - multiline with comments');
is ($node->{content}, $t);

$t = "this is a test ;;; with commen[[MARKUP]]ts! \nand so is this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch;
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'node processed completely - markup in comment');
is ($node->{content}, $t);

$t = "this is a test\nand so is [markup] this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'text processing halted at markup');
is ($node->{content}, $t);
is ($l->prev, ' ');
is ($l->fetch, undef);

$t = "this is a test\nand so is[markup] this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'text processing halted at markup abutting text');
is ($node->{content}, $t);
is ($l->prev, ' ');
is ($l->current, 'this');
is ($l->fetch, undef);

$t = "this is [a~] test\nand s[o] is[markup] this";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'entities caught; processing halted at markup');
is ($node->{content}, $t);
is ($l->prev, ' ');
is ($l->current, 'this');
is ($l->fetch, undef);

$t = "line 1\nline 2\nline3\n\nline4";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'processing halted by para break');
is ($node->{content}, $t);
is ($l->current, "line4");
is ($l->prev, "\n");
is ($l->fetch, undef);

$t = "blah blah blah]]]\nfoo";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'processing halted by closing markup');
is ($node->{content}, $t);

# ok now, real test of halting on proper verbatim closing markup
$t = "blah blah blah\n]]]\nfoo";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
is ($h->status, 0, 'normal ops - 0');
$node = $h->lex($l);
is ($h->status, 1, 'processing halted by closing markup');
is ($node->{content}, "blah blah blah\n");
is ($l->current, "\n");
is ($l->prev, "\n");
is ($l->fetch, "]]]");
